#include "playerskilldialog.h"
#include "gamedialogitem.h"
#include "gamedialogmanager.h"
#include "gamewindow.h"
#include "gameanimation.h"
#include "gametable.h"
#include "player.h"
#include "ghostboarditems.h"
#include <QPainter>
#include <QDebug>

/*
  BottomLessDialog CLASS
*/
BottomLessDialog::BottomLessDialog(GameDialogManager *m, GameDialogBg* f,Player* p)
    :GameDialog(m, f),_pl(p)
{
    _bg = QPixmap(":/common/images/linmeng.jpg");
    setTaoButtons(_pl);
    showAnim->addFade(this, 0, 1, 300);
    hideAnim->addFade(this, 1, 0, 300);
    connect(p,SIGNAL(taoChanged()),this,SLOT(updateRemainTao()));
}

void BottomLessDialog::setTaoButtons(Player *p)
{
    for(int i=0;i<4;i++){

    TaoItemButton* redTao = new TaoItemButton(this,RED,p);
    redTao->moveBy(-(TaoItemButton::WIDTH*(i-1.5)),0);
    rTaoButtons.append(redTao);
    TaoItemButton* yellowTao = new TaoItemButton(this,YELLOW,p);
    yellowTao->moveBy(-(TaoItemButton::WIDTH*(i-1.5)),-TaoItemButton::HEIGHT*2);
    yTaoButtons.append(yellowTao);
    TaoItemButton* greenTao = new TaoItemButton(this,GREEN,p);
    greenTao->moveBy(-(TaoItemButton::WIDTH*(i-1.5)),-TaoItemButton::HEIGHT);
    gTaoButtons.append(greenTao);
    TaoItemButton* blackTao = new TaoItemButton(this,BLACK,p);
    blackTao->moveBy(-(TaoItemButton::WIDTH*(i-1.5)),TaoItemButton::HEIGHT*2);
    blackTaoButtons.append(blackTao);
    TaoItemButton* blueTao = new TaoItemButton(this,BLUE,p);
    blueTao->moveBy(-(TaoItemButton::WIDTH*(i-1.5)),TaoItemButton::HEIGHT);
    blueTaoButtons.append(blueTao);
    }
}

int BottomLessDialog::execDialog(){
    startDialog();
    int r = e.exec();
    //enter local event loop to wait for user input
    return r;
}

QRectF BottomLessDialog::boundingRect() const{
    return QRectF(-_bg.width()/2,-_bg.height()/2, _bg.width(), _bg.height());
}

void BottomLessDialog::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget){
    painter->drawPixmap(-_bg.width()/2,-_bg.height()/2,_bg);
}

void BottomLessDialog::updateRemainTao() {
    QList<int>currentTaoNums = this->getDialogManager()->getGameWindow()->getTable()->getItemManager()->getCurrentTaoNums();
    qDebug() << "Current Tao:" << currentTaoNums;
    int rTaoNum = currentTaoNums.at(0);
    for(int i=0;i<rTaoNum;i++)
        rTaoButtons.at(i)->setVisible(false);
    int yTaoNum = currentTaoNums.at(1);
    for(int i=0;i<yTaoNum;i++)
        yTaoButtons.at(i)->setVisible(false);
    int gTaoNum = currentTaoNums.at(2);
    for(int i=0;i<gTaoNum;i++)
        gTaoButtons.at(i)->setVisible(false);
    int blackTaoNum = currentTaoNums.at(3);
    for(int i=0;i<blackTaoNum;i++)
        blackTaoButtons.at(i)->setVisible(false);
    int blueTaoNum = currentTaoNums.at(4);
    for(int i=0;i<blueTaoNum;i++)
        blueTaoButtons.at(i)->setVisible(false);
    this->update();
}

